local zhiluan = fk.CreateSkill {
  name = "rfenghou_3d15j__zhiluan",
  max_branches_use_time = {
    ["slash"] = {
      [Player.HistoryTurn] = 1
    },
    ["jink"] = {
      [Player.HistoryTurn] = 1
    },
  }
}

local U = require "packages.utility.utility"

zhiluan:addEffect("viewas", {
  pattern = "slash,jink",
  mute_card = false,
  min_card_num = 1,
  card_filter = function(self, player, to_select, selected)
    if Fk:currentRoom():getCardArea(to_select) == Card.PlayerHand then
      local mark_table = player:getMark("@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo")
      if mark_table == 0 then return "" end
      return #selected < #mark_table.list_names
    end
  end,
  interaction = function(self, player)
    local all_names = {}
    for _, name in ipairs({ "slash", "jink" }) do
      if zhiluan:withinBranchTimesLimit(player, name, Player.HistoryTurn) then
        table.insert(all_names, name)
      end
    end
    local names = player:getViewAsCardNames(zhiluan.name, all_names)
    return names[1] and UI.CardNameBox { choices = names, all_choices = all_names } or nil
  end,
  prompt = function(self)
    if self.interaction.data == "slash" then
      return "#rfenghou_3d15j__zhiluan:::#rfenghou_3d15j__zhiluan-target_hao:#rfenghou_3d15j__zhiluan-target_hao2:slash"
    elseif self.interaction.data == "jink" then
      return "#rfenghou_3d15j__zhiluan:::#rfenghou_3d15j__zhiluan-target_mo:#rfenghou_3d15j__zhiluan-target_mo2:jink"
    end
    return "#rfenghou_3d15j__zhiluan0"
  end,
  view_as = function(self, player, cards)
    if not self.interaction.data or #cards == 0 then return end
    local mark_table = player:getMark("@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo")
    if mark_table == 0 or #cards > #mark_table.list_names then return end
    local c = Fk:cloneCard(self.interaction.data)
    c.skillName = zhiluan.name
    c:addFakeSubcards(cards)
    return c
  end,
  history_branch = function(self, player, data)
    return data.interaction_data
  end,
  before_use = function(self, player, use)
    local room = player.room
    local cards = use.card.fake_subcards
    local gt_or_lt = self.interaction.data == "slash"

    local mark_table = table.clone(player:getMark("@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo"))
    if mark_table == 0 then
      player:addSkillUseHistory(zhiluan.name)
      player:addSkillBranchUseHistory(zhiluan.name, self.interaction.data)
      return ""
    end
    local name_tables = {}
    for index, value in ipairs(mark_table.list_names) do
      table.insert(name_tables, value .. ":::" .. index) --- 必须做区分，不然看不见了
    end
    local list_cards = mark_table.list_cards
    local orig_buffs = #mark_table.buff
    for _, cid in ipairs(cards) do
      local prompt = "#rfenghou_3d15j__zhiluan-choose:::" .. Fk:getCardById(cid):toLogString() .. ":#rfenghou_3d15j__zhiluan-target_" .. (gt_or_lt and "hao" or "mo")
      local swap_name = U.askForChooseCardList(room, player, name_tables, list_cards, 1, 1, zhiluan.name, prompt, true, false)[1]
      if not swap_name then
        player:addSkillUseHistory(zhiluan.name)
        player:addSkillBranchUseHistory(zhiluan.name, self.interaction.data)
        return ""
      end
      local idx = table.indexOf(name_tables, swap_name)
      list_cards[idx][(gt_or_lt and 1 or 2)] = cid
    end

    -- 神秘借用extra函数

    local remain = Fk.skill_skels["rfenghou_3d15j__guanwei"].extra.rfenghou_3d15j__setGuanweiMark(player, {
      target = player,
      card_table = list_cards,
      skill_name = zhiluan.name,
      skip = true,
    })
    room:obtainCard(player, remain, true, fk.ReasonExchange, player, zhiluan.name)
    local new_mark_table = player:getMark("@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo")
    if mark_table == 0 then
      player:addSkillUseHistory(zhiluan.name)
      player:addSkillBranchUseHistory(zhiluan.name, self.interaction.data)
      return ""
    end
    local buff = #new_mark_table.buff
    if gt_or_lt then
      if buff <= orig_buffs then
        player:addSkillUseHistory(zhiluan.name)
        player:addSkillBranchUseHistory(zhiluan.name, self.interaction.data)
        return ""
      end
    else
      if buff >= orig_buffs then
        player:addSkillUseHistory(zhiluan.name)
        player:addSkillBranchUseHistory(zhiluan.name, self.interaction.data)
        return ""
      end
    end
  end,
  enabled_at_play = function(self, player)
    return not player:isKongcheng() and player:getMark("@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo") ~= 0
  end,
  enabled_at_response = function(self, player, response)
    return not player:isKongcheng() and player:getMark("@[rfenghou__haomo_cardlist]rfenghou_3d15j__panjun_haomo") ~= 0
  end
}, { check_skill_limit = true })

Fk:loadTranslationTable{
  ["rfenghou_3d15j__zhiluan"] = "治乱",
  [":rfenghou_3d15j__zhiluan"] = "每回合每项限一次，当你需要使用或打出【杀】/【闪】时，你可用任意张手牌替换等量的“毫”/“末”，若“观微”中增益数因此变多/变少，你视为使用或打出之。",

  ["#rfenghou_3d15j__zhiluan0"] = "治乱：用任意张手牌替换等量的“毫”/“末”，若“观微”中增益数因此变多/变少，你视为使用或打出【杀】/【闪】",
  ["#rfenghou_3d15j__zhiluan"] = "治乱：用任意张手牌替换等量的%arg，若“观微”中增益数因此%arg2，你视为使用或打出%arg3",

  ["#rfenghou_3d15j__zhiluan-choose"] = "治乱：请为 %arg 选择一组被替换 %arg2 的“毫末”",

  ["#rfenghou_3d15j__zhiluan-target_hao"] = "“毫”",
  ["#rfenghou_3d15j__zhiluan-target_mo"] = "“末”",
  ["#rfenghou_3d15j__zhiluan-target_hao2"] = "变多",
  ["#rfenghou_3d15j__zhiluan-target_mo2"] = "变少",
}

return zhiluan
